<template>
  <div class="app-container">
    <!-- 查询区域 -->
    <el-form :model="queryParams" :ref="queryFormRef" inline v-show="showSearch" label-width="auto" @keyup.enter.native="handleQuery">
        <el-form-item label="关键字" prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable />
        </el-form-item>
        <el-form-item label="登录账号" prop="usernameKey">
          <el-input v-model="queryParams.usernameKey" placeholder="请输入关键字" clearable />
        </el-form-item>
        <el-form-item label="状态" prop="status" label-width="40px" style="width: 170px;">
          <lam-dict-select v-model="queryParams.status" dictCode="user_status" placeholder="状态" clearable style="width: 120px;"/>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="tool-bar-btn">
      <el-button type="primary" icon="el-icon-plus"  @click="handleAdd" v-hasPerms="['sys:user:add']">新增</el-button>
      <el-button type="success" icon="el-icon-edit"  :disabled="single" @click="handleUpdate" v-hasPerms="['sys:user:edit']">修改</el-button>
      <el-button type="info" icon="el-icon-upload2"  @click="handleImport" v-hasPerms="['sys:user:export']">导入</el-button>
      <el-button type="danger" icon="el-icon-delete"  :disabled="multiple" @click="handleDelete" v-hasPerms="['sys:user:remove']">删除</el-button>
      <el-button type="warning" icon="el-icon-download"  @click="handleExport" v-hasPerms="['sys:user:export']">导出</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>

    <selected-info :count="selectedRows.length" :onClearSelected="onClearSelected" />
    <el-table :ref="dataTableRef" v-loading="loading" :data="dataList" stripe border @selection-change="onSelectChange"
          :default-sort = "defaultSort" @sort-change="onSortChange" :row-key="rowKey" >
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="登录账号" align="center" prop="username"  width="150" sortable="custom" fixed="left"/>
      <el-table-column label="真实姓名" align="center" prop="realname"  width="120" />
      <el-table-column label="昵称" align="center" prop="nickName"  width="150" />
      <el-table-column label="性别" align="center" prop="sexDictText" width="80"/>
      <el-table-column label="邮箱" align="center" prop="email"  width="180" />
      <el-table-column label="电话" align="center" prop="phone"  width="120" />
      <el-table-column label="状态" align="center" prop="statusDictText" width="80">
        <template slot-scope="{row}"><UserStatus :value="row.status" :text="row.statusDictText" /></template>
      </el-table-column>
      <el-table-column label="用户类型" align="center" prop="userTypeDictText" width="100"/>
      <el-table-column label="新用户" align="center" prop="isNewDictText" width="80"/>
      <el-table-column label="创建人" align="center" prop="createBy"  width="120" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="修改人" align="center" prop="updateBy"  width="120" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="220" >
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)" v-hasPerms="['sys:user:edit']">修改</el-button>
          <el-button type="text" @click="openUserRoleDialog(row)" v-hasPerms="['sys:role:toSelectList:userRole']">
            <i><svg-icon icon-class="team" /></i>角色</el-button>
          <el-dropdown v-hasPerms="['sys:user:remove','sys:user:resetPwd']">
            <el-link type="primary" icon="el-icon-arrow-down" :underline="false" class="el-button--small">更多</el-link>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-hasPerms="['sys:user:remove']">
                <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPerms="['sys:user:resetPwd']">
                <el-button type="text" icon="el-icon-refresh" @click="handleResetPwd(row)" >重置密码</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="!loading" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"  @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <EditUser ref="formDialog" @ok="getList" />

    <!-- 导入对话框 -->
    <ToImport ref="importDialog" @ok="getList"/>
	<!-- 导出对话框 -->
	<ToExport ref="exportDialog" />

    <UserRole ref="userRole" />

  </div>
</template>

<script>
import {httpGet, httpPost, httpDel} from '@/api';
import { IndexListMixin } from '@/components/mixins/IndexListMixin';
import EditUser from './edit';
import ToImport from '@/components/upload/ToImport';
import ToExport from '@/components/download/ToExport';
import UserRole from './userRole';

export default {
  name: "User",
  mixins:[IndexListMixin],
  components: {
    EditUser,
    ToImport,
    ToExport,
    UserRole
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        usernameKey: null,
        realnameKey: null,
        nickNameKey: null,
        pageNum: 1,
        pageSize: 10
      },
      url: {
        resetPwd: '/sys/user/resetPwd',
      	exportXls: '/sys/user/export',
        base: '/sys/user/',
        downloadTemplateUrl: '/sys/user/downloadTemplate',
        uploadUrl: '/sys/user/importData',
      	list: '/sys/user/list'
      }
    };
  },
  created() {
    this.getList();
  },
  mounted(){
  },
  methods: {
    getList() {
      this.loadData(this.url.list);
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.formDialog.toAdd("添加用户信息");
    },

    openUserRoleDialog(row){
      this.$refs.userRole.open(row.id);
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.selectedRowKeys
      this.$refs.formDialog.toUpdate("修改用户信息", httpGet(this.url.base + id));
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.selectedRowKeys
      this.deleteData(this.url.base, ids);
    },

    /** 重置密码按钮操作 */
    handleResetPwd(row){
      const ids = row.id || this.selectedRowKeys
      this.$confirm('确认重置选中用户的密码?', "确认", {type: "warning"}).then(()=>{
        httpPost(this.url.resetPwd, {id: ids}).then((res) => {
          this.msgSuccess("重置成功！");
        }).catch(function() {});
      });
    },

    /** 导入按钮操作 */
    handleImport() {
      this.$refs.importDialog.openDialog({
        title: '用户信息导入',
        uploadUrl: this.url.uploadUrl,
        downloadTemplateUrl: this.url.downloadTemplateUrl
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      this.exportData('用户信息导出', this.url.exportXls, this.queryParams);
    }
  }
};
</script>
